package org.turnlink.sclm.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.turnlink.sclm.model.Contact;
import org.turnlink.sclm.service.ContactManager;
import org.turnlink.sclm.utils.InvalidSessionException;

@Controller
@RequestMapping("/contact/{format}/{token}/")
public class ContactServiceController extends AbstractServiceController {

	@Autowired
	private ContactManager contactManager;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(@PathVariable String token,
			@PathVariable("format") String format,
			Contact contactMap, HttpServletResponse response) {
		checkSession(token, response);
		this.contactManager.add(contactMap);
		return new ModelAndView(getViewName(format), "contact", contactMap);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(@PathVariable String token,
			@PathVariable("format") String format,
			Contact contactMap, HttpServletResponse response) {
		checkSession(token, response);
		this.contactManager.update(contactMap);
		return null;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ModelAndView delete(@PathVariable String token,
			@PathVariable("format") String format,
			Contact contactMap, HttpServletResponse response) {
		checkSession(token, response);
		this.contactManager.remove(contactMap);
		return null;
	}
	

	@RequestMapping(value = "/batchInsert", method = RequestMethod.POST)
	public ModelAndView batchInsert(@PathVariable String token,
			@PathVariable("format") String format,
			@RequestParam("name") String name,
			@RequestParam("file") MultipartFile file, HttpServletResponse response) {
		checkSession(token, response);
		// TODO finish working on this method so that it works
		return null;
	}

	@RequestMapping(value = "/get/{contactId}", method = RequestMethod.GET)
	public ModelAndView get(@PathVariable String token,
			@PathVariable("format") String format,
			@PathVariable int contactId, HttpServletResponse response) {
		checkSession(token, response);
		Contact contact = contactManager.get(contactId);
		return new ModelAndView(getViewName(format), "contact", contact);
	}

	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public ModelAndView find(@PathVariable String token,
			@PathVariable("format") String format,
			@RequestParam("query") String query, HttpServletResponse response) {
		checkSession(token, response);
		List<Contact> contacts = contactManager.find(query);
		return new ModelAndView(getViewName(format), "contacts", contacts);
	}

	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public ModelAndView count(@PathVariable String token,
			@PathVariable("format") String format,
			@RequestParam("query") String query, HttpServletResponse response) {
		checkSession(token, response);
		int count = contactManager.count(query);
		return new ModelAndView(getViewName(format), "count", count);
	}
	
	@ExceptionHandler(InvalidSessionException.class)
	public String handleInvalidSessionException(InvalidSessionException ex, HttpServletResponse response) throws IOException {
		response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		return null;
	}
	
}
